def lastn[A](n: Int, l: List[A]): A = {
    def lastn1(c: Int, List2: List[A], List1: List[A]): A =
      List1 match {
        case Nil if c > 0 => throw new NoSuchElementException
        case Nil              => List2.head
        case _ :: tail        =>
          lastn1(c - 1,
                   if (c > 0) List2 else List2.tail,
                   tail)
      }
    if (n <= 0) throw new IllegalArgumentException
    else lastn1(n, l, l)
  }
}



